package com.yungu.swift.api.route.v1.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.base.Constants;
import com.yungu.swift.autoconfig.redis.RedisCacheService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.common.service.CommonMessageStatusService;
import com.yungu.swift.common.service.CommonSmsService;
import com.yungu.swift.order.model.param.SecretNoParam;
import com.yungu.swift.order.service.OrderSecretNoService;
import com.yungu.swift.utils.MapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @author Zerrion
 * @description
 */
@RestController
@RequestMapping("/api/v1/common")
@Api(value = "系统通用接口", tags = "系统通用接口")
public class CommonVariableApi extends BaseApi {
    private static final Logger logger = LoggerFactory.getLogger(CommonVariableApi.class);

    @Autowired
    private RedisCacheService redisCacheService;
    @Reference
    private CommonMessageStatusService commonMessageStatusService;
    @Reference
    private CommonSmsService commonSmsService;
    @Reference
    private OrderSecretNoService orderSecretNoService;


    @RequestMapping(value = {"/driver/getGovern"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取司机端未读消息数量", response = ResponseData.class)
    public ResponseData<Map<String, Object>> getDriverGovern(HttpServletRequest request) {
        // 开始查找未读消息数
        Map<String, Object> params = MapUtils.build(4);
        params.put("targetUuid", getUserUid(request));
        // 未读
        params.put("status", 14);
        params.put("driUnreadCount", "driUnreadCount");
        return ResponseData.buildSuccessResponse(MapUtils.build("unreadCount", commonMessageStatusService.count(params).getData()));
    }


//    @RequestMapping(value = {"/identifyCode/send"}, method = {RequestMethod.POST, RequestMethod.GET})
//    @ApiOperation(value = "通用发送验证码", response = ResponseData.class)
//    public ResponseData<Boolean> sendIdentifyCode(
//            @ApiParam(name = "mobile", value = "手机号", required = true) @RequestParam String mobile,
//            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
//        return commonSmsService.sendIdentify(mobile, appid);
//    }

    @RequestMapping(value = {"/identifyCode/send"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "通用发送验证码", response = ResponseData.class)
    public ResponseData<Boolean> sendIdentifyCode(
            @ApiParam(name = "mobile", value = "手机号", required = true) @RequestParam String mobile,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
        return commonSmsService.sendAliIdentify(mobile, appid);
    }

    @RequestMapping(value = {"/order/removePayCache"}, method = {RequestMethod.POST})
    @ApiOperation(value = "清除服务器支付缓存", response = ResponseData.class)
    public ResponseData removePayCache(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单ID", required = true) @RequestParam String orderUuid) {
        String cacheKey = Constants.ORDER_CACHE_KEY + orderUuid;
        logger.info("客户端调用--尝试清除入口缓存锁");
        if (redisCacheService.hasKey(cacheKey)) {
            redisCacheService.delete(cacheKey);
            logger.info("客户端调用--清除尝试清除入口缓存锁");
        } else {
            logger.info("客户端调用--入口缓存锁不存在");
        }
        return ResponseData.buildSuccessResponse("操作完成", null);
    }

    @RequestMapping(value = {"/privacyNum/queryBinding"}, method = {RequestMethod.POST})
    @ApiOperation(value = "查询隐私小号绑定", response = ResponseData.class)
    public ResponseData<Map<String, String>> verifyBingding(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "passengerMobile", value = "乘客手机号", required = true) @RequestParam String passengerMobile,
            @ApiParam(name = "orderUuid", value = "订单ID", required = true) @RequestParam String orderUuid) {
        SecretNoParam secretNoParam = new SecretNoParam();
        secretNoParam.setOrderUuid(orderUuid);
        secretNoParam.setPassengerMobile(passengerMobile);
        return orderSecretNoService.queryBinding(secretNoParam);
    }

}
